//
//  SignUpViewController.m
//  DailyGram
//
//  Created by TiOluwa Olarewaju on 9/2/12.
//  Copyright (c) 2012 MT. All rights reserved.
//

#import "SignUpViewController.h"
#import <Parse/Parse.h>

@interface SignUpViewController ()

@end

@implementation SignUpViewController
@synthesize email;
@synthesize username;
@synthesize password;
@synthesize phone;
@synthesize picture;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [self setEmail:nil];
    [self setUsername:nil];
    [self setPassword:nil];
    [self setPhone:nil];
    [self setPicture:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == username) {
        [username resignFirstResponder];
    }
    if (textField == password) {
        [password resignFirstResponder];
    }
    if (textField == email) {
        [email resignFirstResponder];
    }
    if (textField == phone) {
        [phone resignFirstResponder];
    }
    
    
    return NO;
}


- (IBAction)signUp:(id)sender {
    
    PFUser *user = [PFUser user];
    user.username = username.text;
    user.password = password.text;
    user.email = email.text;
    
    [user setObject:phone.text forKey:@"phone"];
    //[user setObject:picture forKey:@"picture"];
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            [self dismissModalViewControllerAnimated:YES];
            
            
        } else {
            NSLog(@"Login Error: %@", error);
            if (error.code == 202){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                                message:@"Username already taken"
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles: nil];
                [alert show];
            }
        }
    }];
}
@end
